package com.hnhd.eorder.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.GridView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SimpleAdapter;

import com.hnhd.eorder.R;

public class TableDialog_b extends Dialog {
	
	private Context context;

	/**
	 * 包厢餐台网格
	 */
	public GridView gv_table_list;

	/**
	 * 网格填充用的键值对的键数组
	 */
	public String[] from = { "tableName", "tableNum", "tableMaxNum",
			"tableWaiter" };

	/**
	 * 要填充的对象id数组
	 */
	public int[] to = { R.id.tv_table_item_name, R.id.tv_table_item_num,
			R.id.tv_table_item_max_num, R.id.tv_table_item_waiter };

	public TableDialog_b(Context context, int theme) {
		super(context, theme);
		this.context = context;
		setContentView(R.layout.dialog_table);

		gv_table_list = (GridView) findViewById(R.id.gv_table_list);

		this.initGridView(gv_table_list, from, to);

		RadioGroup rg_room_group = (RadioGroup) findViewById(R.id.rg_room_group);
		rg_room_group.setOnCheckedChangeListener(new RadioListener());

	}

	private class RadioListener implements OnCheckedChangeListener {

		public void onCheckedChanged(RadioGroup group, int checkedId) {

			final ProgressDialog progressDialog = new ProgressDialog(context);  
			  
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  


            progressDialog.setMessage("please wait...");  

            progressDialog.setIndeterminate(false);  

            progressDialog.setCancelable(true);  

            progressDialog.show();  
            new Thread() {  
                @Override  
                public void run() {  
                	int count = 0;
                    try {  
                        while (count <= 10) {  
                            // 由线程来控制进度  
                        	progressDialog.setProgress(count++);  
                            Thread.sleep(100);  
                        }  
                        progressDialog.cancel();  
                    } catch (Exception e) {  
                    	progressDialog.cancel();  
                    }  
                }  
            }.start();  

			switch (checkedId) {
			case R.id.rb_room_01:
				initGridView(gv_table_list, from, to);
				break;
			case R.id.rb_room_02:
				initGridView2(gv_table_list, from, to);
				break;
			case R.id.rb_room_03:
				initGridView3(gv_table_list, from, to);
				break;
			}
		}
	}

	private void initGridView(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < 20; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "101");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

	private void initGridView2(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 5; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "102");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

	private void initGridView3(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 5; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "103");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

}
